package com.sxm.yiwei.activitis;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.FWContentImgAdapter;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.tools.ImgSwitcherActivity;
import com.sxm.yiwei.utils.InternetAddressUtis;
import com.sxm.yiwei.views.MyGridView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class FWEditServiseContentActivity extends BaseActivity implements View.OnClickListener{

    private ImageView beautyback;
    private LinearLayout baeutysearch;
    private ImageView backmain;
    private SharedPreferences sharedPreferences;
    private String token;
    private String userId;
    private String appId;
    private TextView fwtype;
    private TextView fename;
    private LinearLayout allcommen;
    private ImageView commen1;
    private ImageView commen2;
    private ImageView commen3;
    private ImageView commen4;
    private ImageView commen5;
    private TextView fwamount;
    private TextView fwawardnumber;
    private TextView fwpaynumber;
    private TextView fwhistoryprice;
    private TextView fwintro;
    private Button fwbt1,fwbt2,fwbt3;
    private TextView fwtip;
    private MyGridView showimgs;
    private MyGridView fwintroimgs;
    private JSONArray showImages;
    private ImageView introimg1,introimg2,introimg3;
    private JSONArray introImages;
    private TextView fwpromise1,fwpromise2,fwpromise3,fwpromise4,fwpromise5;
    private Integer splitindex;
    private String from;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fw_beauty);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        userId = sharedPreferences.getString("userId", "");
        appId = sharedPreferences.getString("appid", "");
        id = getIntent().getStringExtra("id");
        from = getIntent().getStringExtra("from");
        initBeautyView();
        if (from.equals("1")){
            fwtype.setText("编辑服务-出售中");
            fwbt1.setText("编辑");
            fwbt2.setText("展位");
            fwbt3.setText("删除");
            fwbt1.setVisibility(View.VISIBLE);
            fwbt2.setVisibility(View.VISIBLE);
            fwbt3.setVisibility(View.VISIBLE);
        }else if (from.equals("2")){
            fwtype.setText("编辑服务-待上架");
            fwbt1.setText("编辑");
            fwbt2.setText("删除");
            fwbt1.setVisibility(View.VISIBLE);
            fwbt2.setVisibility(View.VISIBLE);
            fwbt3.setVisibility(View.GONE);
        }else {
            fwtype.setText("编辑服务-回收站");
            fwbt1.setText("待上架");
            fwbt2.setText("彻底删除");
            fwbt1.setVisibility(View.VISIBLE);
            fwbt2.setVisibility(View.VISIBLE);
            fwbt3.setVisibility(View.GONE);
        }
        getServiceData(id);

    }

    // 获取服务详情
    private void getServiceData(String id) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETSERVICEDATA);
        params.addHeader("Content-Type","application/json");
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);

        JSONObject object = new JSONObject();
        object.put("serviceId",id);
        params.addParameter("params",object.toJSONString());

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject obj = JSON.parseObject(result);
                String status = obj.getString("status");
                if (status.equals("1")){
                    getData(result);
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(FWEditServiseContentActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
        // 获取数据
    private void getData(String result) {
        JSONObject obj = JSON.parseObject(result);
        String data = obj.getString("data");
        JSONObject obj1 = JSON.parseObject(data);
        String amount = obj1.getString("amount");
        String id = obj1.getString("id");
        String intro = obj1.getString("intro");
        String name = obj1.getString("name");
        String promise = obj1.getString("promise");
        String shopId = obj1.getString("shopId");
        String unit = obj1.getString("unit");
        String volume = obj1.getString("volume");
        String rewardCount = obj1.getString("rewardCount");
        introImages = obj1.getJSONArray("introImages");
        showImages = obj1.getJSONArray("showImages");
        if (rewardCount!=null){
            fwawardnumber.setText(rewardCount);
        }else {
            fwawardnumber.setText("0");
        }
        fwamount.setText(amount);
        fename.setText(name);
        fwpaynumber.setText(volume);
        fwintro.setText(intro);
        if (unit.equals("1")){
            fwtip.setText(" 元/时");
        }else if (unit.equals("2")){
            fwtip.setText(" 元/天");
        }else if (unit.equals("3")){
            fwtip.setText(" 元/月");
        }else {
            fwtip.setText(" 元/单");
        }
        FWContentImgAdapter imgAdapter = new FWContentImgAdapter(FWEditServiseContentActivity.this, showImages);
        showimgs.setAdapter(imgAdapter);
        int itemWidth = (int)(125 * this.getResources().getDisplayMetrics().density);
        int itemSize = showImages.size();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(itemSize * itemWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
        showimgs.setLayoutParams(params);
        showimgs.setNumColumns(itemSize);
        showimgs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent in = new Intent(FWEditServiseContentActivity.this, ImgSwitcherActivity.class);
                in.putExtra("position",position);
                in.putExtra("imgs", showImages);
                startActivity(in);
            }
        });
        if (!promise.equals("")&!promise.equals(null)){

            String[] split = promise.split(",");
            for (int i = 0; i < split.length; i++) {
                splitindex = Integer.valueOf(split[i]);

                switch (splitindex){
                    case 1:
                        fwpromise1.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        fwpromise2.setVisibility(View.VISIBLE);
                        break;
                    case 3:
                        fwpromise3.setVisibility(View.VISIBLE);
                        break;
                    case 4:
                        fwpromise4.setVisibility(View.VISIBLE);
                        break;
                    case 5:
                        fwpromise5.setVisibility(View.VISIBLE);
                        break;
                }
            }

        }else {}
        int size = introImages.size();
        switch (size){
            case 1:
                introimg1.setVisibility(View.VISIBLE);
                x.image().bind(introimg1,InternetAddressUtis.IMG_URL+introImages.get(0));
                break;
            case 2:
                introimg1.setVisibility(View.VISIBLE);
                x.image().bind(introimg1,InternetAddressUtis.IMG_URL+introImages.get(0));
                introimg2.setVisibility(View.VISIBLE);
                x.image().bind(introimg2,InternetAddressUtis.IMG_URL+introImages.get(1));
                break;
            case 3:
                introimg1.setVisibility(View.VISIBLE);
                x.image().bind(introimg1,InternetAddressUtis.IMG_URL+introImages.get(0));
                introimg2.setVisibility(View.VISIBLE);
                x.image().bind(introimg2,InternetAddressUtis.IMG_URL+introImages.get(1));
                introimg3.setVisibility(View.VISIBLE);


                x.image().bind(introimg3,InternetAddressUtis.IMG_URL+introImages.get(2));
                break;
        }
    }

    private void initBeautyView() {
        beautyback = (ImageView) findViewById(R.id.beauty_back);
        backmain = (ImageView) findViewById(R.id.fweditor_backmain);
        fwtype = (TextView) findViewById(R.id.editfwcontent_type);
        fename = (TextView) findViewById(R.id.fweditor_fwname);
        allcommen = (LinearLayout) findViewById(R.id.fwedit_allcommen);
        commen1 = (ImageView) findViewById(R.id.fweditor_fwcommen1);
        commen2 = (ImageView) findViewById(R.id.fweditor_fwcommen2);
        commen3 = (ImageView) findViewById(R.id.fweditor_fwcommen3);
        commen4 = (ImageView) findViewById(R.id.fweditor_fwcommen4);
        commen5 = (ImageView) findViewById(R.id.fweditor_fwcommen5);
        fwamount = (TextView) findViewById(R.id.fwedit_fwamount);
        fwawardnumber = (TextView) findViewById(R.id.fwedit_fwawardnumber);
        fwpaynumber = (TextView) findViewById(R.id.fwedit_fwpaynumber);
        fwhistoryprice = (TextView) findViewById(R.id.fwedit_fwhistoryprice);
        fwintro = (TextView) findViewById(R.id.fwedit_fwintro);
        fwtip = (TextView) findViewById(R.id.fwedit_fwtip);
        fwbt1 = (Button) findViewById(R.id.fweditor_bt1);
        fwbt2 = (Button) findViewById(R.id.fweditor_bt2);
        fwbt3 = (Button) findViewById(R.id.fweditor_bt3);
        showimgs = (MyGridView) findViewById(R.id.fweditor_showimgs);
        introimg1 = (ImageView) findViewById(R.id.fweditor_introimg1);
        introimg2 = (ImageView) findViewById(R.id.fweditor_introimg2);
        introimg3 = (ImageView) findViewById(R.id.fweditor_introimg3);
        fwpromise1 = (TextView) findViewById(R.id.fweditor_promise1);
        fwpromise2 = (TextView) findViewById(R.id.fweditor_promise2);
        fwpromise3 = (TextView) findViewById(R.id.fweditor_promise3);
        fwpromise4 = (TextView) findViewById(R.id.fweditor_promise4);
        fwpromise5 = (TextView) findViewById(R.id.fweditor_promise5);

        beautyback.setOnClickListener(this);
        backmain.setOnClickListener(this);
        fwbt1.setOnClickListener(this);
        fwbt2.setOnClickListener(this);
        fwbt3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.beauty_back:
                finish();
                break;
            case R.id.fweditor_backmain:
                startActivity(new Intent(FWEditServiseContentActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.fweditor_bt1:

                if (from.equals("1")){// 编辑
                    editService(id);
                }else  if (from.equals("2")){// 编辑
                    editService(id);
                }else  if (from.equals("3")){ // 待上架
                    toUPservice(id);
                }
                break;
            case R.id.fweditor_bt2:

                if (from.equals("1")){// 展位
                    setServiceView(id);
                }else  if (from.equals("2")){//删除
                    deleteServiceItem(id);
                }else  if (from.equals("3")){//彻底删除
                    toDeleteService(id);
                }
                break;
            case R.id.fweditor_bt3:

                if (from.equals("1")){// 删除
                    deleteServiceItem(id);
                }else  if (from.equals("2")){

                }else  if (from.equals("3")){

                }
                break;
        }
    }

    // 设置展位
    private void setServiceView(String id){
        Intent i = new Intent(FWEditServiseContentActivity.this, ShopSetImageViewActivity.class);
        i.putExtra("serviceid",id);
        startActivity(i);
    }

    // 待上架服务
    private void toUPservice(String serviceId) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.SHOPTOUPSERVICE);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject object = new JSONObject();
        object.put("serviceId",serviceId);
        params.setBodyContent(object.toJSONString());

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject obj = JSON.parseObject(result);
                String status = obj.getString("status");
                if (status.equals("1")){
                    Toast.makeText(FWEditServiseContentActivity.this, "已上架，请耐心等待审核", Toast.LENGTH_SHORT).show();
                }

            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(FWEditServiseContentActivity.this,ex);

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    // 彻底删除服务
    private void toDeleteService(String serviceId) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.SHOPTODELETESERVICE);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject object = new JSONObject();
        object.put("serviceId",serviceId);
        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject obj = JSON.parseObject(result);
                String status = obj.getString("status");
                if (status.equals("1")){
                    finish();
                    Toast.makeText(FWEditServiseContentActivity.this, "已彻底删除服务", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(FWEditServiseContentActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    // 编辑
    private void editService(String id){
        Intent intent = new Intent(FWEditServiseContentActivity.this, FWSellServiceActivity.class);
        intent.putExtra("id",id);
        intent.putExtra("from","bianji");
        startActivity(intent);
    }

    // 删除某一服务
    private void deleteServiceItem(final String position) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.SHOPDELETESERVICE);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject deleteobj = new JSONObject();
        deleteobj.put("serviceId",position);
        params.setBodyContent(deleteobj.toJSONString());

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject obj = JSON.parseObject(result);
                String status = obj.getString("status");
                if (status.equals("1")){
                    Toast.makeText(FWEditServiseContentActivity.this, "已删除服务", Toast.LENGTH_SHORT).show();
                    finish();
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
}
